package com.openbravo.data.loader;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Session
{
  private String m_surl;
  private String m_suser;
  private String m_spassword;
  private Connection m_c;
  private boolean m_bInTransaction;
  public final SessionDB DB;

  public Session(String paramString1, String paramString2, String paramString3)
    throws SQLException
  {
    this.m_surl = paramString1;
    this.m_suser = paramString2;
    this.m_spassword = paramString3;
    this.m_c = null;
    this.m_bInTransaction = false;
    connect();
    this.DB = getDiff();
  }

  public void connect()
    throws SQLException
  {
    close();
    this.m_c = ((this.m_suser == null) && (this.m_spassword == null) ? DriverManager.getConnection(this.m_surl) : DriverManager.getConnection(this.m_surl, this.m_suser, this.m_spassword));
    this.m_c.setAutoCommit(true);
    this.m_bInTransaction = false;
  }

  public void close()
  {
    if (this.m_c != null)
      try
      {
        if (this.m_bInTransaction)
        {
          this.m_bInTransaction = false;
          this.m_c.rollback();
          this.m_c.setAutoCommit(true);
        }
        this.m_c.close();
      }
      catch (SQLException localSQLException)
      {
      }
      finally
      {
        this.m_c = null;
      }
  }

  public Connection getConnection()
    throws SQLException
  {
    if (!this.m_bInTransaction)
      ensureConnection();
    return this.m_c;
  }

  public void begin()
    throws SQLException
  {
    if (this.m_bInTransaction)
      throw new SQLException("Already in transaction");
    ensureConnection();
    this.m_c.setAutoCommit(false);
    this.m_bInTransaction = true;
  }

  public void commit()
    throws SQLException
  {
    if (this.m_bInTransaction)
    {
      this.m_bInTransaction = false;
      this.m_c.commit();
      this.m_c.setAutoCommit(true);
    }
    else
    {
      throw new SQLException("Transaction not started");
    }
  }

  public void rollback()
    throws SQLException
  {
    if (this.m_bInTransaction)
    {
      this.m_bInTransaction = false;
      this.m_c.rollback();
      this.m_c.setAutoCommit(true);
    }
    else
    {
      throw new SQLException("Transaction not started");
    }
  }

  public boolean isTransaction()
  {
    return this.m_bInTransaction;
  }

  private void ensureConnection()
    throws SQLException
  {
    int i;
    try
    {
      i = (this.m_c == null) || (this.m_c.isClosed()) ? 1 : 0;
    }
    catch (SQLException localSQLException)
    {
      i = 1;
    }
    if (i != 0)
      connect();
  }

  public String getURL()
    throws SQLException
  {
    return getConnection().getMetaData().getURL();
  }

  private SessionDB getDiff()
    throws SQLException
  {
    String str = getConnection().getMetaData().getDatabaseProductName();
    if ("HSQL Database Engine".equals(str))
      return new SessionDBHSQLDB();
    if ("MySQL".equals(str))
      return new SessionDBMySQL();
    if ("PostgreSQL".equals(str))
      return new SessionDBPostgreSQL();
    if ("Oracle".equals(str))
      return new SessionDBOracle();
    if ("Apache Derby".equals(str))
      return new SessionDBDerby();
    return new SessionDBGeneric(str);
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.loader.Session
 * JD-Core Version:    0.6.0
 */